package com.springcloud.common.core.exception;

/**
 * <p>  </p>
 *
 * @author mori
 * @since 2019/4/15 10:25
 */
public enum ApiErrorCode implements IErrorCode {
    /**
     * 失败
     */
    FAILED(-1, "系统异常"),
    /**
     * 成功
     */
    SUCCESS(0, "执行成功"),

    SYSTEM_BUSY(40001, "系统繁忙,请稍候再试"),

    GATEWAY_NOT_FOUND_SERVICE(40002, "服务未找到"),

    GATEWAY_ERROR(40003, "网关异常"),

    GATEWAY_CONNECT_TIME_OUT(40004, "网关超时"),

    ARGUMENT_NOT_VALID(40005, "请求参数校验不通过"),

    UPLOAD_FILE_SIZE_LIMIT(40006, "上传文件大小超过限制"),

    DUPLICATE_PRIMARY_KEY(40007, "唯一键冲突");

    private final Integer code;
    private final String msg;

    ApiErrorCode(final Integer code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ApiErrorCode fromCode(Integer code) {
        ApiErrorCode[] ecs = ApiErrorCode.values();
        for (ApiErrorCode ec : ecs) {
            if (ec.getCode().equals(code)) {
                return ec;
            }
        }
        return SUCCESS;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }
}
